<?php
namespace app\shop\controller;
use app\base\model\CityM;
use app\rbac\model\AdminM;
use app\shop\model\AttributeM;
use app\shop\model\GoodCatM;
use app\shop\model\GoodsM;
use think\Db;
use think\Session;
use app\base\controller\Base;

class Goods extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 新增
     * @return mixed
     */
    public function add()
    {
        $admin_id = session('admin_id');
        if($this->request->isPost())
        {
            $data = $this->request->post();
            $model = new GoodsM();
            $res = $model->add($data);
            if($res)
            {
                $this->success('新增成功',url('shop/Goods/lst'));
            }
            else
            {
                $this->error($model->msg);
            }
        }
        //地区
        $cityModel = new CityM();
        $cityData = $cityModel->getCityByLevel();
        //店铺
        $shopData = Db::table('shop_shop')->where(['is_usable'=>1,'admin_id'=>$admin_id])->field('id,title')->select();
        //品牌
        $brandData = Db::table('shop_brand')->field('id,name')->select();
        //分类
        $catModel = new GoodCatM();
        $catData = $catModel->getCatTree();
        //类型
        $typeData = Db::table('shop_type')->field('id,name')->select();
        $this->assign(array(
            '_page_title'=>'添加商品',
            '_page_title_english'=>'Goods add',
            '_btn_title'=>'商品列表',
            '_btn_link'=>url('lst'),
            '_page_ico'=>'iconfa-shopping-cart',
            'shopData'=>$shopData,
            'brandData'=>$brandData,
            'catData'=>$catData,
            'typeData'=>$typeData,
            'cityData'=>$cityData,
        ));
        return $this->fetch();
    }



    /**
     * 编辑页面
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        //先判断权限
        $admin_id = session('admin_id');
        $adminModel = new AdminM();
        $res = $adminModel->isAuth($id,'shop_goods');
        if($res === false)
        {
            $this->error('权限不够');
        }
        if($this->request->isPost())
        {
            $data = $this->request->post();
            $model = new GoodsM();
            $res = $model->edit($data);
            if($res)
            {
                $this->success('修改成功',url('shop/Goods/lst'));
            }
            else
            {
                $this->error($model->msg);
            }
        }
        //地区
        $cityModel = new CityM();
        $cityData = $cityModel->getCityByLevel();
        //获取数据
        $data = Db::table('shop_goods')
            ->alias('g')
            ->join('city c1','c1.id = g.shi_id','LEFT')
            ->join('city c2','c2.id = g.xian_id','LEFT')
            ->where('g.id',$id)
            ->field('g.*,c1.name as shi_name,c2.name as xian_name')
            ->find();
        //商品属性
        $attrData = Db::table('shop_attribute')->where('type_id',$data['type_id'])->select();
        //商品已有属性
        $attrModel = new AttributeM();
        $gaData=$attrModel->getGoodsAttr($id);
        //商品描述
        //商品详情
        $good_desc = Db::table('shop_goods_desc')->where(['goods_id'=>$id,'is_default'=>1,'is_usable'=>1])->field('id,desc_content')->find();
        //其他描述
        $other_desc = Db::table('shop_goods_desc')->where(['goods_id'=>$id,'is_usable'=>1,'is_default'=>0])->field('id,desc_name,desc_content')->select();
        //店铺
        $shopData = Db::table('shop_shop')->where(['is_usable'=>1,'admin_id'=>$admin_id])->field('id,title')->select();
        //品牌
        $brandData = Db::table('shop_brand')->field('id,name')->select();
        //分类
        $catModel = new GoodCatM();
        $catData = $catModel->getCatTree();
        //类型
        $typeData = Db::table('shop_type')->field('id,name')->select();

        //商品相册列表
        $gpData = Db::table('shop_goods_pic')->where('goods_id',$id)->field('id,small_pic')->select();

        $this->assign(array(
            '_page_title'=>'编辑商品',
            '_page_title_english'=>'Goods edit',
            '_btn_title'=>'商品列表',
            '_btn_link'=>url('lst'),
            '_page_ico'=>'iconfa-shopping-cart',
            'data'=>$data,
            'shopData'=>$shopData,
            'brandData'=>$brandData,
            'catData'=>$catData,
            'typeData'=>$typeData,
            'gaData'=>$gaData,
            'attrData'=>$attrData,
            'gpData'=>$gpData,
            'good_desc'=>$good_desc,
            'other_desc'=>$other_desc,
            'cityData'=>$cityData,
        ));
        return $this->fetch();
    }

    /**
     * 获取全部数据
     * @return mixed
     */
    public function lst()
    {
        $model = new GoodsM();
        $data = $model->lst(15);
        // 获取分页显示
        $page = $data->render();
        $this->assign(array(
            '_page_title'=>'商品列表',
            '_page_title_english'=>'Goods list',
            '_btn_title'=>'添加商品',
            '_btn_link'=>url('add'),
            '_page_ico'=>'iconfa-shopping-cart',
            'data'=>$data,
            'page'=>$page,
        ));
        return $this->fetch();
    }


    /**
     * 删除数据
     * @param $id
     */
    public function del()
    {
        //先判断权限
        $admin_id = session('admin_id');
        $adminModel = new AdminM();
        $res = $adminModel->isAdmin($admin_id);
        if($res === false)
        {
            return json(['code'=>-1,'msg'=>'没有权限']);
        }
        $id = $this->request->post('id');
        $model = new GoodsM();
        $model->del($id);
        return json(['code'=>$model->code,'msg'=>$model->msg]);
    }

    /**
     * 审核
     * @param $id
     */
    public function review()
    {
        //先判断权限
        $admin_id = session('admin_id');
        $adminModel = new AdminM();
        $res = $adminModel->isAdmin($admin_id);
        if($res === false)
        {
            return json(['code'=>-1,'msg'=>'没有权限']);
        }
        $id = $this->request->post('id');
        $model = new GoodsM();
        $model->review($id);
        return json(['code'=>$model->code,'msg'=>$model->msg]);
    }





}
